import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:pp_uikit/pp_refresh.dart';
import 'package:pp_uikit/state_layout/pp_state_layout_mixin.dart';

class PPPageController extends FullLifeCycleController
    with StateMixin, PPStateLayoutMixin, PPRefreshMixin, FullLifeCycleMixin {
  var scaffoldKey = GlobalKey<ScaffoldState>();

  Map? parameters;

  void openEndDrawer() {
    scaffoldKey.currentState?.openEndDrawer();
  }

  void closeEndDrawer() {
    Get.back();
  }

  void onPageReady() {}

  @mustCallSuper
  @override
  void onInit() async {
    super.onInit();
  }

  @override
  void onReady() async {
    _onReady();
  }

  void _onReady() async {
    super.onReady();
    // var previousRoute = Get.previousRoute;
    // if (previousRoute.isEmpty) {
    //   try {
    //     parameters =
    //         await ChannelManager.invokeMethod(ChannelMethod.parameters);
    //   } catch (e) {
    //     PPLog.d("Flutter ChannelManager.invokeMethod parameters error = $e");
    //   }
    // } else {
    //   if (Platform.isIOS) {
    //     // 关闭原生侧滑
    //     ChannelManager.invokeMethod(ChannelMethod.closeNativePop);
    //   }
    // }
    var getParameters = Get.parameters;
    var getArguments = Get.arguments;
    if (parameters == null) {
      parameters = <dynamic, dynamic>{};
      if (getParameters.isNotEmpty) {
        parameters?.addAll(getParameters);
      }
      if (getArguments != null && getArguments is Map) {
        parameters?.addAll(getArguments);
      }
    } else {
      parameters?.addAll(getParameters);
      if (getArguments != null && getArguments is Map) {
        parameters?.addAll(getArguments);
      }
    }
    onPageReady();
  }

  void hideKeyboard(BuildContext context) {
    SystemChannels.textInput.invokeMethod('TextInput.hide');
  }

  @override
  void onDetached() {
    // 应用在运行但与组件分离 ;
  }

  @override
  void onHidden() {
    //
  }

  @override
  void onInactive() {
    // 应用进入非活动状态 ;
  }

  @override
  void onPaused() {
    // 应用进入后台 ;
  }

  @override
  void onResumed() {
    // 应用进入前台 ;
  }
}
